﻿using Sc.Domain;
using Sc.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Script.Serialization;

namespace Sc.Web.Controllers.WebApi
{
    public class UserController : ApiController
    {
        private UserService service = new UserService();
        /// <summary>
        /// 登陆处理信息
        /// </summary>
        /// <param name="model">登录账号</param>
        /// <returns></returns>
        [HttpPost]
        public object Login([FromBody]UserLogin model)
        {
            var data = service.UserLoginForApp(model.LoginCode, model.LoginPwd, model.Token, model.OsType);
            if (data == null)
            {
                return "error";
            }
            JavaScriptSerializer json = new JavaScriptSerializer();
            var temp = json.Serialize(data);
            return new HttpResponseMessage { Content = new StringContent(temp, System.Text.Encoding.UTF8, "application/json") }; ;
        }
        /// <summary>
        /// 获取我的申请
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object GetPageData()
        {
            int id = Convert.ToInt32(HttpContext.Current.Request["userId"]);
            int pageSize = Convert.ToInt32(HttpContext.Current.Request["pageSize"]);
            int pageIndex = Convert.ToInt32(HttpContext.Current.Request["pageIndex"]);
            int type = Convert.ToInt32(HttpContext.Current.Request["type"]);
            JavaScriptSerializer json = new JavaScriptSerializer();
            var tmp = service.GetList(id, pageSize, pageIndex, type);
            var data = json.Serialize(tmp);
            if (tmp == null)
            {
                data = "[]";
            }
            return new HttpResponseMessage { Content = new StringContent(data, System.Text.Encoding.UTF8, "application/json") };
        }
        /// <summary>
        /// 获取我的申请
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object UpdatePwd()
        {
            int id = Convert.ToInt32(HttpContext.Current.Request["userId"]);
            string pwd = HttpContext.Current.Request["pwd"];
            var mp = service.UpdatePassword(id, pwd);
            return new HttpResponseMessage { Content = new StringContent(mp.ToString(), System.Text.Encoding.UTF8, "application/json") };
        }
        [HttpGet]
        public void Test()
        {
            CommPushMessage ms = new CommPushMessage();
            var tl = HttpContext.Current.Request["t"];
            var c = HttpContext.Current.Request["c"];
            var id = Convert.ToInt32(HttpContext.Current.Request["id"]);
            var p = HttpContext.Current.Request["p"];
            var token = HttpContext.Current.Request["tk"];
            MessageParam pm = new MessageParam()
            {
                Title = tl,
                Content = c,
                BusinessId = id,
                Perfix = p,
                UserAndroidToken = new List<string>() { token }
            };
            ms.PushMessageForAndroid(pm);
        }
    }
}
